using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SubSonic;
using System.Text;

public partial class Article : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                LoadArticleConent(int.Parse(Request.QueryString["id"].ToString()));
            }
        }
    }

    protected void LoadArticleConent(int article_id)
    {
        string culture = Utility.GetCulture(this.Page);
        this.linkHome.NavigateUrl = "~/default.aspx?" + culture;

        Gospel.DB.Article article = new Gospel.DB.Article(article_id);
        if (article.IsLoaded)
        {
            if (String.Equals(culture, "cn", StringComparison.CurrentCultureIgnoreCase))
            {
                this.divContent.InnerHtml = Utility.ConvertToHtml(article.ArticleContentCn);
            }
            else
            {
                this.divContent.InnerHtml = Utility.ConvertToHtml(article.ArticleContentTw);
            }


            StringBuilder url = new StringBuilder("");
            StringBuilder name = new StringBuilder("");
            LoadLocation(article_id, url, name);

            string[] arrayUrl = url.ToString().Split("|".ToCharArray());
            string[] arrayName = name.ToString().Split("|".ToCharArray());
            DataTable dt = new DataTable();
            dt.Columns.Add("key");
            dt.Columns.Add("value");
            DataRow row;

            for (int i = arrayUrl.Length-1; i >= 0; i--)
            {
                if (!String.IsNullOrEmpty((arrayName[i].Trim())))
                {
                    row = dt.NewRow();
                    row["key"] = arrayName[i].Trim();
                    row["value"] = arrayUrl[i].Trim();
                    dt.Rows.Add(row);
                }
            }

            dt.AcceptChanges();
            this.repeatLocation.DataSource = dt.DefaultView;
            this.repeatLocation.DataBind();

            LoadAttachment(article_id);

            LoadChild(article_id);
        }

    }

    protected void LoadLocation(int article_id, StringBuilder url, StringBuilder name)
    {
        string culture = Utility.GetCulture(this.Page);
        Gospel.DB.Article article = new Gospel.DB.Article(article_id);
        if (article.IsLoaded)
        {
            if (String.Equals(culture, "cn", StringComparison.CurrentCultureIgnoreCase))
            {
                name.Append(article.ArticleTitleCn + "|");
            }
            else
            {
                name.Append(article.ArticleTitleTw + "|");
            }
            url.Append("article.aspx?" + culture + "&id=" + article.ArticleId.ToString() + "|");

            if (article.FatherId != 0)
            {
                LoadLocation(article.FatherId, url, name);
            }
        }
    }

    protected void LoadAttachment(int article_id)
    {
        string culture = Utility.GetCulture(this.Page);
        Query q = Gospel.DB.ArticleAttachment.CreateQuery();
        q.WHERE(Gospel.DB.ArticleAttachment.Columns.ArticleId, article_id);
        if (String.Equals(culture, "cn", StringComparison.CurrentCultureIgnoreCase))
        {
            q.SelectList = "*, " + Gospel.DB.ArticleAttachment.Columns.AttachmentNameCn + " as attachment_name";
            q.OrderBy = OrderBy.Asc(Gospel.DB.ArticleAttachment.Columns.AttachmentNameCn);
        }
        else
        {
            q.SelectList = "*, " + Gospel.DB.ArticleAttachment.Columns.AttachmentNameTw + " as attachment_name";
            q.OrderBy = OrderBy.Asc(Gospel.DB.ArticleAttachment.Columns.AttachmentNameTw);
        }

        DataSet ds = new DataSet();
        ds = q.ExecuteDataSet();
        if (ds.Tables.Count == 1)
        {
            this.repeatAttachment.DataSource = ds.Tables[0].DefaultView;
            this.repeatAttachment.DataBind();

            if (ds.Tables[0].Rows.Count != 0)
            {
                this.penalAttachment.Visible = true;
            }
        }
    }

    protected void LoadChild(int article_id)
    {
        string culture = Utility.GetCulture(this.Page);
        Query q = Gospel.DB.Article.CreateQuery();
        q.WHERE(Gospel.DB.Article.Columns.FatherId, article_id);
        if (String.Equals(culture, "cn", StringComparison.CurrentCultureIgnoreCase))
        {
            q.SelectList = "'Article.aspx?" + culture + "&id=' + Convert(varchar(10)," + Gospel.DB.Article.Columns.ArticleId + ") as parameter, " + Gospel.DB.Article.Columns.ArticleTitleCn + " as article_title";
            q.OrderBy = OrderBy.Asc(Gospel.DB.Article.Columns.ArticleTitleCn);
        }
        else
        {
            q.SelectList = "'Article.aspx?" + culture + "&id=' + Convert(varchar(10)," + Gospel.DB.Article.Columns.ArticleId + ") as parameter, " + Gospel.DB.Article.Columns.ArticleTitleTw + " as article_title";
            q.OrderBy = OrderBy.Asc(Gospel.DB.Article.Columns.ArticleTitleTw);
        }

        DataSet ds = new DataSet();
        ds = q.ExecuteDataSet();
        if (ds.Tables.Count == 1)
        {
            this.repeatChild.DataSource = ds.Tables[0].DefaultView;
            this.repeatChild.DataBind();

            if (ds.Tables[0].Rows.Count != 0)
            {
                this.panelChild.Visible = true;
            }
        }
    }

}
